<?php
/**
 *
 * Author: TheSmile
 * Date: 2024/11/4 17:11
 * Email: <594652523@qq.com>
 */

namespace App\Exports;

use App\Models\Campus;
use App\Models\CourseDuty;
use App\Models\CourseOrder;
use App\Models\Estimate;
use App\Models\Users;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithMapping;

class TeacherEnroll extends ExcelExporter implements WithMapping, WithColumnWidths
{
    protected $fileName = '教师库.xlsx';

    protected $columns = [
        'id'     => '教师ID',
        'name'   => '教师姓名',
        'mobile' => '电话',
        'age'    => '积分',//随便找个模型不用的字段
        'num'    => '已报学员',//随便找个模型不用的字段
        'sex'    => '满意度'//随便找个模型不用的字段
    ];

    public function columnWidths(): array
    {
        return ['B' => 20, 'C' => 20];
    }


    public function map($teacher): array
    {
        return [
            $teacher->id,
            $teacher->name,
            $teacher->mobile,
            self::getScore($teacher),
            self::getUserCount($teacher),
            self::getManScore($teacher)
        ];
    }


    protected static function getScore($teacher)
    {
        $value = Users::where('teacher_id', $teacher->id)->value('score');
        return (string)$value ?? "0";
    }

    protected static function getUserCount($teacher)
    {
        $value = CourseOrder::whereIn('course_id', function ($q) use ($teacher) {
            $q->where('teacher_id', $teacher->id)->select('id')->from('course');
        })->where('status', '!=', -1)->where('status', '!=', 1)->where('status', '!=', 4)->distinct('user_id')->count();
        return (string)$value ?? "0";
    }

    protected static function getManScore($teacher)
    {
        $value = Estimate::where('teacher_id', $teacher->id)->avg('teacher_score');
        return max($value, "0");
    }
}
